package com.library.infrastructure;

import com.library.application.BookService;
import com.library.domain.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

//@Component
public class DataInitializer implements CommandLineRunner {

    private final BookService bookService;

//    @Autowired
    public DataInitializer(BookService bookService) {
        this.bookService = bookService;
    }

    @Override
    public void run(String... args) {
        // 检查是否已经有图书，如果没有则初始化
        if (bookService.getAllBooks(PageRequest.of(0, 1)).isEmpty()) {
            for (int i = 1; i <= 100; i++) {
                Book book = new Book();
                book.setTitle("Book " + i);
                book.setAuthor("Author " + i);
                try {
                    bookService.addBook(book, null); // 传入 null 作为 MultipartFile 参数
                } catch (Exception e) {
                    System.err.println("Error adding book: " + e.getMessage());
                }
            }
            System.out.println("Initialized 100 books.");
        }
    }
}